package com.jiantong.web.open.app.user;

import com.jiantong.user.model.Area;
import com.jiantong.user.service.AreaReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by mz on 31/5/17.
 *
 */
@Slf4j
@OpenBean
public class AreaApis {

    @RpcConsumer
    private AreaReadService areaReadService;


    @OpenMethod(
            key = "query.area.list",
            paramNames = {"areaCode","grade"},
            httpMethods = RequestMethod.GET
    )
    public List<Area> queryAreaList(String areaCode , String grade){
        if(grade == null){
            log.error("信息不全无法查找 没有地区级别");
            throw new OPClientException("信息不全无法查找 没有地区级别");
        }
        if(Integer.parseInt(grade) > 1 && areaCode == null){
            log.error("信息不全无法查找 没有省级别");
            throw new OPClientException("信息不全无法查找 没有省级别");
        }
        Response<List<Area>> listResponse = areaReadService.queryList(areaCode,grade);

        if(!listResponse.isSuccess()){
            log.error("failed to find area list");
            throw new OPServerException("failed to find area list :areaCode = " + areaCode + "grade = " + grade);
        }
        return listResponse.getResult();
    }
}
